////////////////////////////////////////////////////////////////////////
//                                                                    //
//   GGGGGGGG  EEEEEEEE  RRRRRRRR   MM     MM       A       NN    NN  //
//  GG         E         RR     RR  MMM   MMM      A A      NNN   NN  //
//  GG         EEEEEE    RR     RR  MM M M MM     A   A     NNNN  NN  //
//  GG    GGG  E         RRRRRRRR   MM  M  MM    AAAAAAA    NN NN NN  //
//  GG     GG  E         RR    RR   MM     MM   A       A   NN  NNNN  //
//   GGGGGGG   EEEEEEEE  RR     RR  MM     MM  A         A  NN   NNN  //
//                                                                    //
//   CCCCC    OOOOO   DDDDDD    EEEEEEEE                              //
//  CC   CC  O     O  DD   DD   E                                     //
//  CC       O     O  DD    DD  EEEEEE                                //
//  CC       O     O  DD    DD  E                                     //
//  CC   CC  O     O  DD   DD   E                                     //
//   CCCCC    OOOOO   DDDDDD    EEEEEEEE                              //
//                                                                    //
////////////////////////////////////////////////////////////////////////

package gameCopiler;
import java.util.Formatter;

class FileFixer {
   private Formatter f1;
   private read r = new read();
   String clean(String txt)   {
       //calls simple to remove the one line coments then calls multiple to
       //remove the large coments then return its value
        return multiple(simple(txt));
   }
   String multiple(String txt){///remove the long coments
      String temp = "";///temp var to save the new string
      boolean coment=false;///to save if contains a starting coment
      if( txt.contains("/</")){////look for an starting coment
          coment = true;////sets coment to true
          for(int i= 0;i<txt.indexOf("/</");i++){/// adds the words before the coment
                temp+= txt.charAt(i);///adds to temp the char
          }
          while(coment==true){///do while ending coment hasnt found
             if(txt.contains("/>/")){/// look for ending tag
                 coment=false;//// if found set coment to false
                 for(int i= txt.indexOf("/>/")+3;i<txt.length();i++){///adds the words after the coment
                     temp += txt.charAt(i);/// adds to temp the char
                 }
             } else if (r.HasNext()) { // if end coment is not in that line check if there
                                       // are more lines then read the next line
                 txt=r.NextLine();///read the next line
             }else{///if end of the file has reached and ending coment hasnt found make error
                 System.out.println("error chec your coment tags");////print error
                 coment=false;////set coment to false so it stop looking for coments
             }
           }
           temp = multiple(temp);// after the changes calls itsef to check if there are
                                 //more coments in that line
           return temp;///then return temp
      } else {///if it dont contains any starting coment tag just return the text
           return txt +"\n";///return the text with an end line
      }
    }
   String simple(String txt)  {///remove the short coments
      String temp = "";///temp var to save the new string
      if( txt.contains("//")){///check if there are some coment
           for(int i= 0;i<txt.indexOf("//");i++){///adds the words before the coment
                temp+= txt.charAt(i);///adds to temp the character
           }
           return temp;////then return temp value
      } else {////if there is no coment
           return txt ;///return txt value
      }
    }
   void openFile()         {////open the read an writer file
      try {///its nessesary to use try
         r.Open("C:/Users/german/Desktop/prueva.hwe");///open the reader file with the document called  prueva.hwe
         ////open the writer file with the document called noComents.hwe
         f1 = new Formatter("C:/Users/german/Desktop/noComents.hwe");
      } catch(Exception e) {///if there is a problem
         System.out.println("File doesn't exists");////print that the file dosent exists
      }

   }
   void cleanFile()        {////remove coments
      while(r.HasNext()) {////check for every line in the file
        String txt=r.NextLine();////set txt the value of the next line
        f1.format("%s",clean(txt));////calls clean and save the value to the writer file
      }
   }
   void closeFiles()       {///close files
      r.Close();///close reader file
      f1.close();////close writer file
   }
   void adjustFile()       {
      try {///its nessesary to use try
         r.Open("C:/Users/german/Desktop/noComents.hwe");////read the noComents file as a reader
         f1 = new Formatter("C:/Users/german/Desktop/noSpaces.hwe");///read the noSpaces file as a wirter
      } catch(Exception e) {///if there is an error
         System.out.println("File doesn't exists");/////print the file dosent exixt
      }
      while(r.HasNext()) {////while there are more words to read in the file
        String txt=r.Next();////save in txt the value of the next word
        if(txt.contains(";")){///see if there is an ;
          for(int i= 0;i<txt.length();i++)/// for every character of txt
              if(txt.charAt(i)== ';' )///see if there is an ; character
                 f1.format("%s\n", txt.charAt(i));///save in to the write file the char with an end line
              else///if there is not an ; char
                 f1.format("%s", txt.charAt(i));////save in to the write file the char
        } else /// if it dont contains a ; character
          f1.format("%s ", txt);///save to the writer the value of txt
      }
   }
   void lexer()            {////not working 
      try {
         r.Open("no spaces.hwe");
      } catch(Exception e) {
         System.out.println("File doesn't exists");
      }

   }
}